<?php

class RisksController extends Controller
{
	public function actionIndex()
	{
		$pid = Yii::app()->request->getParam('pid');
		$risks = Risk::model()->findAllByAttributes(array('risk_project_id' => $pid));
		
		$this->render('index', array('risks' => $risks));
	}

	// -----------------------------------------------------------
	// Uncomment the following methods and override them if needed
	/*
	public function filters()
	{
		// return the filter configuration for this controller, e.g.:
		return array(
			'inlineFilterName',
			array(
				'class'=>'path.to.FilterClass',
				'propertyName'=>'propertyValue',
			),
		);
	}

	public function actions()
	{
		// return external action classes, e.g.:
		return array(
			'action1'=>'path.to.ActionClass',
			'action2'=>array(
				'class'=>'path.to.AnotherActionClass',
				'propertyName'=>'propertyValue',
			),
		);
	}
	*/
	public function actionAddRisk() {
		$this->layout = 'ajax/main.php';
		// get param
		$risk_source = Yii::app()->request->getPost('risk_source'); 
		$probability = Yii::app()->request->getPost('probability');
		$impact = Yii::app()->request->getPost('impact');
		$riskCost = Yii::app()->request->getPost('riskCost');
		$riskSchedule = Yii::app()->request->getPost('riskSchedule');
		$riskPerformance = Yii::app()->request->getPost('riskPerformance');
		$mitigation = Yii::app()->request->getPost('mitigation');
		
		
		// model
		$risk = new Risk();
		$risk->risk_source = $risk_source;
		$risk->risk_probability_value = $probability;
		$risk->risk_impact_value = $impact;
		$risk->risk_impact_cost = $riskCost;
		$risk->risk_impact_schedule = $riskSchedule;
		$risk->risk_impact_performance = $riskPerformance;
		$risk->risk_mitigation = $mitigation;
		$risk->risk_created_date = new CDbExpression('NOW()');
		$risk->risk_project_id = Yii::app()->request->getParam('pid');
		$risk->risk_posted_by_user_id = Yii::app()->user->id;
		$risk->save();
		
		$this->render('addRisk', array('risk'=>$risk));
	}
	public function actionEditRisk() {
		$this->layout = 'ajax/main.php';
		// get param
		$rid = Yii::app()->request->getPost('rid');
		$risk_source = Yii::app()->request->getPost('risk_source'); 
		$probability = Yii::app()->request->getPost('probability');
		$impact = Yii::app()->request->getPost('impact');
		$riskCost = Yii::app()->request->getPost('riskCost');
		$riskSchedule = Yii::app()->request->getPost('riskSchedule');
		$riskPerformance = Yii::app()->request->getPost('riskPerformance');
		$mitigation = Yii::app()->request->getPost('mitigation');
		
		// get model
		$risk = Risk::model()->findByPk($rid);
		$risk->risk_source = $risk_source;
		$risk->risk_probability_value = $probability;
		$risk->risk_impact_value = $impact;
		$risk->risk_impact_cost = $riskCost;
		$risk->risk_impact_schedule = $riskSchedule;
		$risk->risk_impact_performance = $riskPerformance;
		$risk->risk_mitigation = $mitigation;
		$risk->risk_updated_date = new CDbExpression('NOW()');
		$risk->save();
		
		$this->render('editRisk', array('risk'=>$risk));
	}
	public function actionDelete() {
		$rid = Yii::app()->request->getPost('rid');
        $risk = new Risk();
        $risk->deleteByPk($rid);
        $risk->save();
	}
}